package com.boyuan.huijiaoyu;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.boyuan.huijiaoyu.R;
import com.example.contactfragment.ContactService;
import com.magicsoft.app.helper.PhoneCallHelper;
import com.magicsoft.app.helper.StringUtils;
import com.magicsoft.app.helper.PhoneCallHelper.PhoneCallListener;
import com.magicsoft.app.helper.ToastHelper;
import com.magicsoft.app.wcf.listener.PostRecordResponseListener;

public class AboutWeiTownActivity extends BaseActivity implements
		OnClickListener {

	private Button btnBack;
	private TextView txt_title;
	private boolean isJumpOutOfThirdApp = false;
	private EditText ed1;
	private EditText ed2;
	private ContactService contactService;
	private String user_name;
	private String titleback;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about_weitown_activity);
		prepareView();
	}

	private void prepareView() {
		Intent intent = getIntent();
		titleback = intent.getStringExtra("titleback");
		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		user_name = sp.getString("username", "");
		ed1 = (EditText) findViewById(R.id.editText1);
		ed2 = (EditText) findViewById(R.id.editText2);
		TextView back = (TextView) findViewById(R.id.myback_shezi);
		back.setText(titleback);
		findViewById(R.id.lay_save).setOnClickListener(this);
		findViewById(R.id.relativeLayout1).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		case R.id.lay_save:
			Btn_save();
			break;
		case R.id.relativeLayout1:
			finish();
			break;
		default:
			break;
		}
	}

	private void Btn_save() {
		String mima = ed1.getText().toString().trim();
		String mima1 = ed2.getText().toString().trim();
		if (StringUtils.isEmpty(mima) || mima.length() < 6
				|| mima.length() > 16) {
			ed1.setError("请输入6-16位的旧密码");
			return;
		}

		if (StringUtils.isEmpty(mima1) || mima1.length() < 6
				|| mima1.length() > 16) {
			ed2.setError("请输入6-16位的新密码");
			return;
		}

		if (mima.equals(mima1)) {
			ToastHelper.showMsg(this, "新密码不能和旧密码一样", false);
			return;
		}
		changePassword(mima, mima1);
	}

	private void changePassword(String mima, String mima1) {

		if (contactService == null) {
			contactService = new ContactService(this);
		}
		String mima00 = getMD5(mima);
		String mima01 = getMD5(mima1);
		contactService.changeMima(user_name, mima00, mima01,
				new PostRecordResponseListener() {

					@Override
					public void onFinish(String succString) {
						ToastHelper.showMsg(AboutWeiTownActivity.this, "密码修改成功！", false);
						finish();
					}

					@Override
					public void onFailed(String failString) {
						ToastHelper.showMsg(AboutWeiTownActivity.this, failString, false);
					}
				});

	}

	public String getMD5(String info) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(info.getBytes("UTF-8"));
			byte[] encryption = md5.digest();

			StringBuffer strBuf = new StringBuffer();
			for (int i = 0; i < encryption.length; i++) {
				if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
					strBuf.append("0").append(
							Integer.toHexString(0xff & encryption[i]));
				} else {
					strBuf.append(Integer.toHexString(0xff & encryption[i]));
				}
			}

			return strBuf.toString();
		} catch (NoSuchAlgorithmException e) {
			return "";
		} catch (UnsupportedEncodingException e) {
			return "";
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		if (isJumpOutOfThirdApp) {
			isJumpOutOfThirdApp = false;
			isBackIntoTheFrontDesk = false;
		}
		super.onResume();
	}

	@Override
	public void backClicked() {
		// TODO
		if (isShowingLoading()) {
			hideLoading();

		} else {
			isBackAllowed = true;
		}
	}

}
